<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input class="input" v-model="form.account" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" v-model.trim="form.mobile" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" v-model="form.code" type="text" placeholder="请输入验证码" />
        <span class="code" @click="sendCode()">{{countTime === 0 ?'发送验证码' : `${countTime}秒后尝试`}}</span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" v-model.trim="form.password" type="password" placeholder="请输入密码" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" v-model.trim="form.rePassword" type="password" placeholder="请确认密码" />
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit()">立即提交</a>
  </div>
</template>

<script>
import { reactive } from 'vue'
import { message } from '@/components/Message'
import { reqUserQQPatchCode, reqUserQQPatchLogin } from '@/api/user'

import { useCountDown } from '@/compositions'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
// reqUserQQBindCode   reqUserQQBindLogin
export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    //   表单
    const form = reactive({
      account: '',
      mobile: '',
      code: '',
      password: '',
      rePassword: ''
    })

    const router = useRouter()
    const store = useStore()

    const { countTime, start } = useCountDown(60)
    // 点击发送验证吗
    const sendCode = async () => {
      // 手机验证
      if (!/^1[3-9]\d{9}$/.test(form.mobile)) return message({ text: '手机号格式错误' })
      // 发请求=================
      const { result } = await reqUserQQPatchCode(form.mobile)
      console.log(result)
      //  倒计时
      start()
    }

    const submit = async () => {
      // 校验密码
      if (form.password !== form.rePassword) return message({ text: '手机号不一致' })
      //   发送请求=================
      const { result } = await reqUserQQPatchLogin({
        //   QQid
        unionId: props.unionId,
        // 用户信息
        ...form
      })
      console.log(result)
      //   存到vuex
      store.commit('user/setProfile', result)
      // 合并购物车
      await store.dispatch('cart/asyncMergeLocalCart')
      // 跳转首页
      router.push('/')
      // 提示用户
      message({ type: 'success', text: 'qq绑定成功' })
    }

    return { form, sendCode, countTime, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
